import React from 'react';
import {observer, inject} from 'mobx-react';

import {Button, confirm, Radios, toast} from 'amis';
import type {IHomeStore} from "../../store/HomeStore";
import {RouteComponentProps} from "react-router";

@inject('homeStore')
@observer
export default class HomeHeader extends React.Component<{ homeStore: IHomeStore } & RouteComponentProps> {

    constructor(props: any) {
        super(props);
    }

    componentDidMount() {

    }

    resetDemoDatas() {
        const {homeStore, history} = this.props;
        confirm('将会恢复到原始状态，仅限当前浏览器不影响其他人，是否继续？').then(confirmed => {
            if (confirmed) {
                homeStore.resetDemoDatas()
                toast.success('重置成功')
                history.push('/pages/root')

                window.location.reload()
            }
        });
    }

    render() {
        const {homeStore} = this.props;

        return (
            <div>
                <div className={`a-Layout-headerBar`}>
                    <div className="hidden-xs pull-left p-t-sm px-2">
                        <div style={{width: 180, textAlign: 'center', fontSize: 20}}>Ant-Framework</div>
                    </div>
                    <div className="hidden-xs p-t-sm pull-left px-2">

                        <Radios
                            className="inline m-r-xs" value={homeStore.isPreview ? 0 : 1}
                            options={[{label: '预览', value: 0}, {label: '设计', value: 1}]}
                            onChange={(option: any) => homeStore.setPreview(option.value == 0)}
                        />

                        {homeStore.isPreview ? null : (
                            <React.Fragment>
                                <Button
                                    size="sm"
                                    level="info"
                                    className="m-r-xs"
                                    onClick={() => homeStore.setAddPageModalVisible(true, 'same')}
                                >
                                    新增同级页面
                                </Button>
                                <Button
                                    size="sm"
                                    level="info"
                                    className="m-r-xs"
                                    onClick={() => homeStore.setAddPageModalVisible(true, 'sub')}
                                >
                                    新增子级页面
                                </Button>
                            </React.Fragment>
                        )}
                    </div>

                    <div className="hidden-xs p-t-sm pull-right px-2">
                        <React.Fragment>
                            <Button
                                size="sm"
                                className="m-r-xs"
                                onClick={() => this.resetDemoDatas()}
                            >
                                重置数据
                            </Button>
                            <Button
                                size="sm"
                                level="info"
                                className="m-r-xs"
                                onClick={() => location.href = 'ant/admin.html'}
                            >
                                控制台
                            </Button>
                        </React.Fragment>
                    </div>
                </div>
            </div>
        );
    }
}
